<?php


namespace app\manage\controller;

use think\Log;

class MsgConfig extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $map = [];
        $infos = model('SystemConfig')->getInfosByMap($map, '', false);

        return $this->fetch('',[
            'infos' => $infos,
        ]);
    }

    public function edit()
    {
        $id = input('id');
        if(!$id) return self::showReturnCode(1002,'id 错误');
        $user = model('SystemConfig')->getInfoByMap(['id'=>$id]);
        return self::showJsonReturnCodeData('1001',$user,'');
    }

    public function save()
    {
        if(request()->isAjax()){
            try{

                $id = input('post.id');
                $save_data['_key'] = input('post._key');
                $save_data['_value'] = input('post._value');
                $save_data['remark'] = input('post.remark');
                $save_data['extra'] = input('post.extra');

                if(isset($id) && $id){
                    $save_data['id'] = $id;
                }
                $result = $this->editData(false,'','SystemConfig',$save_data);
                if($result['code'] == 1001){
                    if(isset($id) && $id){
                        $result['msg'] = '修改配置成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'修改配置【'.$save_data['_key'].'】成功');
                    }else{
                        $result['msg'] = '添加配置成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'添加配置【'.$save_data['_key'].'】成功');
                    }
                }

                return $result;
            }catch (\Exception $e){
                Log::error($e->getMessage());
                return self::showReturnCode(1011,$e->getMessage());
            }
        }else{
            return self::showReturnCode(1002);
        }
    }

    public function save2()
    {
        if(request()->isAjax()){
            try{

                $id = input('post.id');
                $save_data['extra'] = input('post._value');


                if(isset($id) && $id){
                    $save_data['id'] = $id;
                }
                $result = $this->editData(false,'','SystemConfig',$save_data);
                if($result['code'] == 1001){
                    if(isset($id) && $id){
                        $result['msg'] = '修改配置成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'修改配置成功');
                    }else{
                        $result['msg'] = '添加配置成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'添加配置成功');
                    }
                }

                return $result;
            }catch (\Exception $e){
                Log::error($e->getMessage());
                return self::showReturnCode(1011,$e->getMessage());
            }
        }else{
            return self::showReturnCode(1002);
        }
    }

    public function status()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $res = model('SystemConfig')->save(['status'=>input('post.status')],['id'=>$id]);
            if($res){
                $this->RecordSystemLog($this->uid,$this->behavior,'状态操作成功,操作ID: '.$id);
                return $this->showReturnCode(1001);
            }else{
                return $this->showReturnCode(1009,'操作失败');
            }
        }else{
            return $this->showReturnCode(1002);
        }
    }
}